package com.example.azheng.rxjavamvpdemo.mvp.presenter;

import com.example.azheng.rxjavamvpdemo.base.BasePresenter;
import com.example.azheng.rxjavamvpdemo.bean.BaseArrayBean;
import com.example.azheng.rxjavamvpdemo.bean.banner.Banner;
import com.example.azheng.rxjavamvpdemo.bean.notice.Notice;
import com.example.azheng.rxjavamvpdemo.mvp.contract.HomeContract;
import com.example.azheng.rxjavamvpdemo.mvp.model.HomeModel;
import com.example.azheng.rxjavamvpdemo.net.RxScheduler;

import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * GitHub：https://github.com/RookieExaminer
 * Email：wei.azheng@foxmail.com
 * Description：
 */
public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter {

    private HomeContract.Model model;

    public HomePresenter() {
        model = new HomeModel();
    }

    @Override
    public void notice() {

        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.notice()
                .compose(RxScheduler.<BaseArrayBean<Notice>>Flo_io_main())
                .as(mView.<BaseArrayBean<Notice>>bindAutoDispose())
                .subscribe(new Consumer<BaseArrayBean<Notice>>() {
                    @Override
                    public void accept(BaseArrayBean<Notice> bean) throws Exception {
                        mView.onNoticeSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void banner() {

        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.banner()
                .compose(RxScheduler.<BaseArrayBean<Banner>>Flo_io_main())
                .as(mView.<BaseArrayBean<Banner>>bindAutoDispose())
                .subscribe(new Consumer<BaseArrayBean<Banner>>() {
                    @Override
                    public void accept(BaseArrayBean<Banner> bean) throws Exception {
                        mView.onBannerSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });

    }

}
